﻿


$(function () {
    _vmBase = new viewModel();

    _vmBase.init();
})

function viewModel() {
    var me = this;
    VueViewModelBase.call(me);
    viewModelExtend.call(me);


    me.tableListUrl = _getTableListUrl;
    me.addUrl = _updateModelUrl;
    me.delUrl = _deleteModelUrl;
    me.tabObj = $('#tb_menus');

    //重写方法
    me.init = function () {

        me.bindTree(_getMenuTreeUrl,"menuTree");

        me.initTableList();

        //添加监听对象
        me.addMonitor();

        //注册icon 单击事件
        me.registerIconClick();

        me.initMenuParent();


    }

    me.initTableList = function () {
        var options = {
            search: false,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'url',
                    title: 'Url地址'
                },
                {
                    field: 'parentName',
                    title: '父级菜单'
                },
                {
                    field: 'icon',
                    title: '图标',
                    formatter: function (value, row, index) {
                        return '<i class="' + row.icon + '"></i>';
                    }
                },
                {
                    field: 'sortIndex',
                    title: '序号',
                },
                {
                    field: 'indexId',
                    title: '节点序号',
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function (value, row, index) {
                        var enabledLabel = '<span class="label label-success"><i class="fa fa-unlock"></i>' + _enabledStr + '</span>';
                        var disabledLabel = '<span class="label label-default"><i class="fa fa-lock"></i>' + _disabledStr + '</span>';
                        if (row.status) {
                            return enabledLabel;
                        } else {
                            return disabledLabel;
                        }
                    }
                },
                {
                    field: 'id',
                    title: '操作',
                    formatter: function (value, row, index) {

                        var enabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onEnabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledUrl + '\',\'' + _disabledUrl + '\',event)">' + _enabledStr + '</a>';
                        var disabledEle = '<a href="javascript:void(0)" onclick="_vmBase.onDisabledClick(this,' + index + ',\'' + row.id + '\',\'' + _enabledUrl + '\',\'' + _disabledUrl + '\',event)">' + _disabledStr + '</a>';

                        if (row.status) {
                            return disabledEle;
                        } else if (!row.status) {
                            return enabledEle;
                        }

                    }
                }
            ]
            
        }
        me.initList(options);

    }
        

    me.save = function (callback) {
        if (!$('#form1').formValid()) {
            return false;
            callback(false);
        }

        var model = vueModel.$data.entityObj;

        //执行成功
        $dh.post(me.addUrl, model, function (result) {
            if (result.Status == ResultStatus.OK) {
                //刷新菜单树
                
                me.refreshTree(_getMenuTreeUrl, "menuTree");
                me.initMenuParent();

                callback(result, true);

            } else {
                callback(result, false);

            }


        });

    }

    me.delete = function (callback) {
        if (!vueModel.selectedData) {

            $dh.msg(_SelectADataMsg); //请选择一条数据
        } else {
            var selectRows = me.selections;
            var ids = [];
            for (var idIndex = 0; idIndex < selectRows.length; idIndex++) {
                ids.push(selectRows[idIndex].id);
            }

            $dh.confirm(_deleteRemand, function () {
                //确认删除
                $dh.post(me.delUrl, { ids: ids }, function (result) {
                    if (result.Status == ResultStatus.OK) {

                        me.refreshTree(_getMenuTreeUrl, "menuTree");
                        me.initMenuParent();

                        callback(result, true);

                    } else {
                        callback(result, false);

                    }
                });

            });

        }

        //callback(true);
    }
}

function viewModelExtend() {
    var me = this;

    me.addMonitor = function (monitorObj) {
        var targetObj = vueModel.entityObj;

        if (monitorObj != null && monitorObj != undefined) {
            me.addMonitorObj(targetObj, monitorObj);
        } else {
            var addObj = {
                Id: '',
                Name: '',
                ParentId: '00000000-0000-0000-0000-000000000000',
                Url:'',
                Icon: '',
                Status: true,
                Description: '',
                SortIndex:0
            };
            me.addMonitorObj(targetObj, addObj);
        }
    }


    me.openSelectIconWin = function () {

        _layerIndex = layer.open({
            type: 1,
            title: "图标", //不显示标题
            area: ['900px', '600px'], //宽高
            content: $('#selectIconDiv'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            success: function (layero, index) {

            }
        });


    }


    me.registerIconClick = function () {

        $('.bs-glyphicons-list li').on('click', function () {
            var cuEle = $(this).children();
            var cuEleClassName = cuEle.attr('class');

            vueModel.$set(vueModel.entityObj, "Icon", cuEleClassName);

            layer.close(_layerIndex);
        });


    }

    me.initMenuParent = function () {
        var options = {
            value: "id",
            text: "text",
            url: _getMenuParentSelUrl,
            placeholderValue: '00000000-0000-0000-0000-000000000000'
        };
        $('#parentMenuId').bindSelectWithUrl(options, function (result) {

        });
    }

    

}

